//
//  UIBarButtonItem+Extension.swift
//  Weibo10
//
//  Created by male on 15/10/24.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    // MARK: - 便利构造函数

    
    /// 创建一个带标题的和字体大小的UIBarBUttonItem
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - fontSize: 字体大小
    ///   - target: 目标对象
    ///   - action: 方法
    public convenience init(title: String, fontSize: CGFloat, target: Any?, action:String, isBack: Bool = false) {
        
        let button = UIButton(title: title, fontSize: fontSize, normalColor: UIColor.white, highlightColor: UIColor.themeColor)
        button.addTarget(target, action: Selector(action), for: .touchUpInside)
        
        if isBack {  //表明是pop返回键
            
            let BarbuttonImage = "nav_back"
            button.setImage(UIImage.init(named: BarbuttonImage), for: .normal)
            button.frame = CGRect(origin: CGPoint(), size: CGSize(width: 44, height: 44))
            button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -30, bottom: 0, right: 0)
        }
        self.init(customView: button)
    }
    
    public convenience init(image: String?, target: Any?, action:String) {
        
        let button = UIButton()
        button.setImage(UIImage.init(named: image ?? ""), for: .normal)
        button.sizeToFit()
        button.addTarget(target, action: Selector(action), for: .touchUpInside)
        
        self.init(customView: button)
    }
}
